// pages/Supermembers/Supermembers.js
import Toast from '../../miniprogram_npm/_vant-weapp@0.5.7@vant-weapp/toast/toast'; //消息提示组件
var WxParse = require('../../wxParse/wxParse.js');

//直销商品列表背景      列表页
import { getDirectseBg, getDirectList } from '../../config/https'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    cate:'',               //当前id
    vipType:'',           //0不是1是
    loadMore: false,      //加载中提示
    finished: false,      //是否已经请求完全部数据
    list: [],             //数据
    page:0,                 //页数
    backgrounColor:"",      //背景颜色
    category:[],            //分类
    headimg:'',             //背景图片
    showTop:false
  },
  // 点击分类
  onChange(e) {
    if (this.data.loadMore){

    }else{
      console.log(e)
      this.oninit()
      let index = e.detail.index; //当前点击下标
      let category = this.data.category
      this.setData({ cate: category[index].id })
      this.onList()  //拼团订单列表数据
    }
    
  },
  // 跳转详情
  onDistridata(e) {
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/Directsellingto/Directsellingto?id='+id,
    })
  },
  //滚动事件 显示隐藏 箭头
  onPageScroll(e){
     if (e.scrollTop > 500) {
      this.setData({
        showTop: true
      })
    } else {
      this.setData({
        showTop: false
      })
    }
  },
  //点击返回顶部
  goTop() {
    wx.pageScrollTo({
      scrollTop:0,
      duration:300
    })
  },
  //重置加载
  oninit(){
    this.setData({page:0,list:[]})
  },
  onLoad: function (options) {//生命周期函数--监听页面加载
    this.getbeig()
    this.oninit()
    this.onList()
  },
  //背景
  getbeig(){
    let that = this
    getDirectseBg({

    }).then(res=>{
      that.setData({
        vipType:res.member_is_vip,
        backgrounColor: res.backgroundcolor,
        category: res.category,
        headimg: res.headimg
      })
    })
  },
  //页面列表
  onList(){
    var that = this
    that.setData({
      loadMore: true
    })
    getDirectList({
      page: ++that.data.page,
      cate: that.data.cate
    }).then(res => {
      var list = res.list
      list = that.data.list.concat(list)
      if (list.length == 0) {
      } else {
        that.setData({
          finished: res.list.length < res.pagesize ? true : false
        })
      }
      that.setData({
        loadMore: false
      })
      wx.stopPullDownRefresh();
      that.setData({
        list
      })
    })
  },
  onReady: function () {//生命周期函数--监听页面初次渲染完成

  },


  onShow: function () {//生命周期函数--监听页面显示

  },


  onHide: function () {//生命周期函数--监听页面隐藏

  },


  onUnload: function () {//生命周期函数--监听页面卸载

  },


  onPullDownRefresh: function () {//页面相关事件处理函数--监听用户下拉动作

  },


  onReachBottom: function () {//页面上拉触底事件的处理函数
    if (!this.data.finished && !this.data.loadMore) {
      this.onList()
    }
  },


  onShareAppMessage: function () {//用户点击右上角分享
    return {
      title: '满足顾客需求，创造精彩生活 厂家直销，击破底价',
      path: '/pages/Directselling/Directselling?userid=' + wx.getStorageSync('userInfo'), 
      imageUrl: '../../img/2.jpg',  //转发图片 默认当前页面图片
      success: (res) => {//转发成功
        console.log("转发成功", res);
      }
    }
  }
})